/*
 * ************************************************************************
 * 本文件归属于西安协同时光软件有限公司，未经许可，不得擅自使用、拷贝和修改 项目组:金堆城钼业股份有限公司科研项目管理信息系统项目组 版 本: 1.0 日
 * 期：Mar 18, 2010 时 间：10:34:15 AM 作 者： Administrator
 * ************************************************************************
 */
package com.manager.commons;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * TODO 此处描写类的详细信息和功能 *
 * 
 * @author shiyu
 * @since jdk1.6
 */
public class DateUtil {

	public final static long HOUR = 3600000L;

	public final static long DAY = 24 * HOUR;

	// public final static String DEFAULT_FORMAT = "yyyy.MM.dd HH:mm:ss";
	public final static String dateFormat = "yyyy.MM.dd HH:mm:ss";// "yyyy-MM-dd";

	public DateUtil() {
	}

	/**
	 * TODO 将给定字符串转换成为日期
	 * 
	 * @author shiyu
	 * @param yyyymmddhhmmss
	 * @param format
	 * @return
	 */
	public static java.util.Date parseDate(String yyyymmddhhmmss, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);

		try {
			ParsePosition pos = new ParsePosition(0);
			return formatter.parse(yyyymmddhhmmss, pos);
		}
		catch(Exception e) {
			throw new IllegalArgumentException("错误的日期:" + yyyymmddhhmmss + " " + e);
		}
	}

	public java.sql.Timestamp parseTimestamp(String yyyymmddhhmmss) {
		return new java.sql.Timestamp(this.parseDate(yyyymmddhhmmss).getTime());
	}

	/**
	 * TODO 将给定字符串"yyyy.MM.dd HH:mm:ss"转换成为日期
	 * 
	 * @author shiyu
	 * @param yyyymmddhhmmss
	 * @return null if yyyymmddhhmmss为空或NULL
	 */
	public java.util.Date parseDate(String yyyymmddhhmmss) {
		if(yyyymmddhhmmss == null || yyyymmddhhmmss.length() == 0)
			return null;

		SimpleDateFormat formatter = new SimpleDateFormat(this.dateFormat);

		try {
			// 标准日期格式: yyyy.mm.dd hh:mm:ss
			yyyymmddhhmmss = yyyymmddhhmmss.replace('-', '.');
			if(yyyymmddhhmmss.indexOf(":") < 0)
				yyyymmddhhmmss += " 00:00:00";

			ParsePosition pos = new ParsePosition(0);
			return formatter.parse(yyyymmddhhmmss, pos);
		}
		catch(Exception e) {
			throw new IllegalArgumentException("错误的日期:" + yyyymmddhhmmss + " " + e);
		}
	}

	public static String dateToString(java.util.Date date) {
		if(date == null)
			return null;

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

		try {
			return formatter.format(date);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException("日期转换成字符串失败:" + e);
		}

	}
	
	public static String dateToString1(java.util.Date date) {
		if(date == null)
			return null;

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			return formatter.format(date);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException("日期转换成字符串失败:" + e);
		}

	}

	public static String dateToString(java.sql.Timestamp date, String format) {
		return dateToString((java.util.Date) date, format);
	}

	public static String dateToString(java.util.Date date, String format) {
		if(date == null)
			return null;

		SimpleDateFormat formatter = new SimpleDateFormat(format);

		try {
			return formatter.format(date);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException("日期转换成字符串失败:" + e);
		}

	}

	public static String getORACLEdatestring(java.util.Date d) {
		return "to_date('" + dateToString(d) + "','yyyy.mm.dd hh24:mi:ss')";
	}

	public static java.sql.Timestamp nowTime() {
		return new java.sql.Timestamp(System.currentTimeMillis());
	}

	public static SimpleDateFormat nowDate() {

		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf;
	}
	public static int getYear(){
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);	
	}
	public static String getLastYear(){
		String year = String.valueOf(getYear());
		return year.substring(2);
	}
}
